import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import { getInfo } from '@/network/user'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    status: '',
    token: localStorage.getItem('token') || '',
    user: {
      uid: "",
      uname: "",
      uphone: "",
      ubirthday: "",
      uemail: "",
      ugender: "",
      upwd: "",
      uimg: "",
    }
  },
  mutations: {
    setUser(state, payload) {
      state.user = payload;
      localStorage.setItem('token', state.user)
    },
    auth_request(state) {
      state.status = 'loading';
    },
    // mutations里面不能传入多个参数，如果要传入多个属性需要传入payload对象来获取其中的属性
    auth_success(state, token) {
      state.status = 'success';
      state.token = token;
    },
    auth_error(state) {
      state.status = 'error';
    },
    logout(state) {
      state.status = '';
      state.token = '';
      state.user = {
        uid: "",
        uname: "",
        uphone: "",
        ubirthday: "",
        uemail: "",
        ugender: "",
        upwd: "",
        uimg: "",
      }
    },
  },
  actions: {
    Login({ commit }, user) {
      return new Promise((resolve, reject) => {
        commit('auth_request')      // 向后端发送请求，验证用户名密码是否正确，请求成功接收后端返回的token值，利用commit修改store的state属性，并将token存放在localStorage中
        getInfo().then(resp => {
          const token = resp.data.token
          // let user = resp.data.user
          // console.log(resp.data.user);
          commit('setUser', resp.data.user);
          // console.log(resp);
          localStorage.setItem('token', token)          // 每次请求接口时，需要在headers添加对应的Token验证
          axios.defaults.headers.common['Authorization'] = token
          // 更新token
          commit('auth_success', token)
          resolve(resp)
        }).catch(err => {
          console.log(err)
          commit('auth_error')
          localStorage.removeItem('token')
          reject(err)
        })
      })
    },
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        console.log("进入logout")
        commit('logout')
        resolve(response)
      }).catch(err => {
        console.log(err)
        // reject(err)
      })
    }
  },
  getters: {
    // !!将state.token强制转换为布尔值，若state.token存在且不为空(已登录)则返回true，反之返回false    
    isLoggedIn: state => !!state.token,
    authStatus: state => state.status
  }
});

export default store;


        // axios.post('http://localhost:8084/user/info', user)
        //   .then(resp => {
        //     // console.log("success")
        //     const token = resp.data.token
        //     // const user = resp.data.user
        //     state.user = resp.data.user
        //     console.log("后台获取")
        //     console.log(resp);
        //     localStorage.setItem('token', token)          // 每次请求接口时，需要在headers添加对应的Token验证
        //     axios.defaults.headers.common['Authorization'] = token
        //     // 更新token
        //     commit('auth_success', token, user)
        //     resolve(resp)
        //   })
        //   .catch(err => {
        //     console.log("error")
        //     commit('auth_error')
        //     localStorage.removeItem('token')
        //     reject(err)
        //   })/*  */